package com.mh.view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.mh.entity.BookType;
import com.mh.jdbc.entity.IPage;
import com.mh.jdbc.entity.MBean;
import com.mh.jdbc.util.StringUtil;
import com.mh.service.BookTypeService;

public class BookTypeManageFrame extends JInternalFrame {
	private JTable bookTypeTable;
	private BookTypeService bookTypeService = new  BookTypeService();
	private JTextField bookTypeVal;
	private JLabel bookTypeRows = new JLabel("");
	private JLabel bookTypeCurrPage = new JLabel("");
	private JLabel bookTypePages = new JLabel("");
	private JButton provBookType = new JButton("上一页");
	private JButton nextBookType = new JButton("下一页");
	private JTextField bookTypeId;
	private JTextField bookTypeName;
	private JTextArea bookTypeDesc;
	private int current;
	/**
	 * Create the frame.
	 */
	public BookTypeManageFrame() {
		setIconifiable(true);
		setClosable(true);
		getContentPane().setFont(new Font("宋体", Font.PLAIN, 18));
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel lblNewLabel = new JLabel("图书类别查询:");
		
		bookTypeVal = new JTextField();
		bookTypeVal.setColumns(10);
		
		JButton bookTypeSearch = new JButton("查询");
		bookTypeSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookType bookType = new BookType();
				bookType.setBookTypeName(bookTypeVal.getText());
				IPage<BookType> iPage = new IPage<>();
				iPage.setParmBean(bookType);
				iPage.setLikePro("bookTypeName");
				fillTable(iPage);
			}
		});
		bookTypeSearch.setIcon(new ImageIcon(BookTypeManageFrame.class.getResource("/com/mh/images/search.png")));
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addGap(59)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
						.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
						.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
							.addComponent(lblNewLabel)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(bookTypeVal, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE)
							.addGap(141)
							.addComponent(bookTypeSearch))
						.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
							.addComponent(provBookType)
							.addGap(37)
							.addComponent(bookTypeCurrPage)
							.addGap(60)
							.addComponent(bookTypePages)
							.addGap(47)
							.addComponent(bookTypeRows)
							.addPreferredGap(ComponentPlacement.RELATED, 97, Short.MAX_VALUE)
							.addComponent(nextBookType)))
					.addContainerGap(51, GroupLayout.PREFERRED_SIZE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(28)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(bookTypeVal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookTypeSearch))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(provBookType)
						.addComponent(bookTypeCurrPage)
						.addComponent(bookTypePages)
						.addComponent(bookTypeRows)
						.addComponent(nextBookType))
					.addGap(31)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 230, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(56, Short.MAX_VALUE))
		);
		provBookType.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				IPage<BookType> iPage = new IPage<>();
				BookType bookType = new BookType();
				iPage.setParmBean(bookType);
				iPage.setCurPage(StringUtil.getNum(bookTypeCurrPage.getText())-1);
				fillTable(iPage);
			}
		});
		nextBookType.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				IPage<BookType> iPage = new IPage<>();
				BookType bookType = new BookType();
				iPage.setParmBean(bookType);
				iPage.setCurPage(StringUtil.getNum(bookTypeCurrPage.getText())+1);
				fillTable(iPage);
			}
		});
		
		JLabel lblNewLabel_1 = new JLabel("编号:");
		
		JLabel lblNewLabel_2 = new JLabel("图书类别名称:");
		
		bookTypeId = new JTextField();
		bookTypeId.setColumns(10);
		
		bookTypeName = new JTextField();
		bookTypeName.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("图书类别描述:");
		
		bookTypeDesc = new JTextArea();
		
		JButton bookTypeModify = new JButton("修改");
		bookTypeModify.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String booktTypeIdVal =  bookTypeId.getText().trim();
				if(StringUtil.isEmpty(booktTypeIdVal)){
					JOptionPane.showMessageDialog(null, "图书类型Id不能为空！");
					return;
				}else{
					BookType bookType = new BookType();
					bookType.setId(Integer.valueOf(booktTypeIdVal));
					bookType.setBookTypeName(bookTypeName.getText().trim());
					bookType.setBookTypeDesc(bookTypeDesc.getText().trim());
					if(bookTypeService.bookTypeEdit(bookType) > 0){
						JOptionPane.showMessageDialog(null,"图书类型修改成功！");
						IPage<BookType> iPage = new IPage<>();
						iPage.setParmBean(new BookType());
						iPage.setCurPage(current);
						fillTable(iPage);
					}else{
						JOptionPane.showMessageDialog(null,"图书类型修改失败！");
					}
				}
			}
		});
		bookTypeModify.setIcon(new ImageIcon(BookTypeManageFrame.class.getResource("/com/mh/images/edit.png")));
		
		JButton BookTypeDelete = new JButton("删除");
		BookTypeDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int isDel = JOptionPane.showConfirmDialog(null, "是否要执行删除操作？","警告",JOptionPane.YES_NO_OPTION);
				if(isDel == 0){
					String booktTypeIdVal =  bookTypeId.getText().trim();
					if(StringUtil.isEmpty(booktTypeIdVal)){
						JOptionPane.showMessageDialog(null, "图书类型Id不能为空！");
						return;
					}else{
						BookType bookType = new BookType();
						bookType.setId(Integer.valueOf(booktTypeIdVal));
						bookType.setBookTypeName(bookTypeName.getText().trim());
						bookType.setBookTypeDesc(bookTypeDesc.getText().trim());
						if(bookTypeService.bookTypeDelete(bookType) > 0){
							JOptionPane.showMessageDialog(null,"图书类型删除成功！");
							IPage<BookType> iPage = new IPage<>();
							iPage.setParmBean(new BookType());
							iPage.setCurPage(current);
							fillTable(iPage);
						}else{
							JOptionPane.showMessageDialog(null,"图书类型删除失败！");
						}
					}
				}
			}
		});
		BookTypeDelete.setIcon(new ImageIcon(BookTypeManageFrame.class.getResource("/com/mh/images/delete.png")));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(50)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
							.addComponent(bookTypeModify)
							.addPreferredGap(ComponentPlacement.RELATED, 312, Short.MAX_VALUE)
							.addComponent(BookTypeDelete))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel_1)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(bookTypeId, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
							.addComponent(lblNewLabel_2)
							.addGap(18)
							.addComponent(bookTypeName, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel_3)
							.addGap(18)
							.addComponent(bookTypeDesc, GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)))
					.addContainerGap(56, GroupLayout.PREFERRED_SIZE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(21)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(bookTypeId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookTypeName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2))
					.addGap(27)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_3)
						.addComponent(bookTypeDesc, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(BookTypeDelete)
						.addComponent(bookTypeModify))
					.addGap(26))
		);
		panel.setLayout(gl_panel);
		
		bookTypeTable = new JTable();
		bookTypeTable.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				bookTypeTableMousePressed(e);
			}
		});
		bookTypeTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u56FE\u4E66\u7C7B\u522B\u540D\u79F0", "\u56FE\u4E66\u7C7B\u522B\u63CF\u8FF0"
			}
		) {
			Class[] columnTypes = new Class[] {
				Integer.class, Object.class, String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		bookTypeTable.getColumnModel().getColumn(0).setPreferredWidth(54);
		bookTypeTable.getColumnModel().getColumn(1).setPreferredWidth(146);
		bookTypeTable.getColumnModel().getColumn(2).setPreferredWidth(266);
		scrollPane.setViewportView(bookTypeTable);
		getContentPane().setLayout(groupLayout);
		setTitle("图书类别馆里");
		setBounds(100, 100, 726, 569);
		
		bookTypeId.setEnabled(false);
		IPage<BookType> iPage = new IPage<>();
		BookType bookType = new BookType();
		iPage.setParmBean(bookType);
		this.fillTable(iPage);
	}
	/**
	 * 表格点击事件进行处理
	 * @param e
	 */
	protected void bookTypeTableMousePressed(MouseEvent evt) {
		int row = bookTypeTable.getSelectedRow();
		bookTypeId.setText(bookTypeTable.getValueAt(row, 0).toString());
		bookTypeName.setText(bookTypeTable.getValueAt(row, 1).toString());
		bookTypeDesc.setText(bookTypeTable.getValueAt(row, 2).toString());
	}

	//初始化表格
	private void fillTable(IPage<BookType> iPage){
		//初始化按钮
		provBookType.setEnabled(true);
		nextBookType.setEnabled(true);
		
		DefaultTableModel dtm = (DefaultTableModel) bookTypeTable.getModel();
		dtm.setRowCount(0);
		IPage<BookType> iPageDB = bookTypeService.bookTypeFindList(iPage);
		int curPage = iPageDB.getCurPage();
		int totalpage = iPageDB.getTotalpage();
		bookTypeCurrPage.setText("当前页:"+curPage);
		bookTypePages.setText("总页数:"+totalpage);
		bookTypeRows.setText(String.format("总记录数%s条", iPageDB.getRows()));
		if(curPage == 1) provBookType.setEnabled(false);
		if(curPage+1 > totalpage) nextBookType.setEnabled(false);
		current = curPage;
		for(MBean mBean : iPageDB.getResult()){
			BookType bt = (BookType) mBean;
			Vector<Object> vector = new  Vector<>();
			vector.add(bt.getId());
			vector.add(bt.getBookTypeName());
			vector.add(bt.getBookTypeDesc());
			dtm.addRow(vector);
		};
	}
}
